﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExcelFileQueryTool
{
    public partial class frmDatePicker : Form
    {
        public DateTime? SelectedValue { get; private set; }

        public event EventHandler<DatePickerEventArgs> DateSelected;

        public frmDatePicker()
            : this(Point.Empty, null)
        { }

        public frmDatePicker(Point location, string title = null)
        {
            InitializeComponent();

            if (string.IsNullOrEmpty(title) == false)
                this.Text = title;

            if (location == Point.Empty)
                this.StartPosition = FormStartPosition.CenterScreen;
            else
                this.Location = location;

            this.SelectedValue = this.date_picker.GetDisplayRange(false).Start;
        }

        private void date_picker_DateSelected(object sender, DateRangeEventArgs e)
        {
            SelectedValue = e.Start;

            var selectedEvent = this.DateSelected;

            if (selectedEvent != null)
                selectedEvent(sender, new DatePickerEventArgs(this.SelectedValue.Value));

            selectedEvent = null;
        }

        private void frmDatePicker_Deactivate(object sender, EventArgs e)
        {
            if (!Disposing && !this.IsDisposed)
                this.Close();
        }
    }


    public sealed class DatePickerEventArgs : EventArgs
    {
        public DateTime Date { get; private set; }

        public DatePickerEventArgs(DateTime date)
        {
            Date = date;
        }
    }
}
